package com.dingli.admin.controller;

import com.dingli.admin.service.ISystemService;
import com.dingli.framework.entity.Theme;
import com.dingli.framework.entity.User;
import com.dingli.framework.filter.GlobalFilter;
import com.dingli.framework.util.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin("*")
@RestController
@RequestMapping("/system")
@Api(tags = "系统相关")
public class SystemController {

    @Autowired
    private ISystemService iSystemService;

    @GetMapping("/login")
    @ApiOperation("登录")
    public Response<User> login(@ApiParam("账号") @RequestParam Long userId,
                                @ApiParam("密码") @RequestParam String password) {
        return iSystemService.login(userId, password);
    }

    @GetMapping("/getThemeList")
    @ApiOperation("获取主题列表")
    public Response<List<Theme>> getThemeList() {
        return iSystemService.getThemeList();
    }

    @GetMapping("/setUserTheme")
    @ApiOperation("用户主题设置")
    public Response<Object> setUserTheme(@ApiParam("用户ID") @RequestParam Long userId,
                                         @ApiParam("主题ID") @RequestParam Long themeId) {
        return iSystemService.setUserTheme(userId, themeId);
    }

    @RequestMapping("/needLogin")
    @ApiOperation(value = "未登录返回接口", hidden = true)
    public Response<Object> needLogin() {
        return new Response<>(401, "请登录后操作！");
    }

    @RequestMapping("/exit")
    @ApiOperation(value = "登录注销")
    public Response<Object> exit(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        GlobalFilter.onlines.remove(token);
        return new Response<>(200, "注销成功！");
    }

}
